#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, os
from os.path import abspath, dirname, join, exists
from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('accost', 'templates'))


def main():
    if len(sys.argv) != 2:
        print "please make a name of the project"
        sys.exit(1)
    project = sys.argv[1]
    if exists(project):
        print "project %s already exists"%project
        sys.exit(1)
    name = project.lower().replace("-", "_")
    os.mkdir(project)
    os.mkdir(join(project, name))
    os.mkdir(join(project, name, 'views'))
    os.mkdir(join(project, name, 'static'))
    os.mkdir(join(project, name, 'templates'))
    files = [   (join(project, 'manage.py'), 'manage.py'),
                (join(project, name, '__init__.py'), '__init__.py'),
                (join(project, name, 'shortcuts.py'), 'shortcuts.py'),
                (join(project, name, 'models.py'), 'models.py'),
                (join(project, name, 'views', '__init__.py'), 'null'),
                (join(project, name, 'views', 'common.py'), 'common.py'),
                (join(project, name, 'views', 'main.py'), 'main.py'),
            ]
    for filename, tmpl_name in files:
        tmpl = env.get_template(tmpl_name)
        fp = open(filename, 'wb')
        fp.write(tmpl.render(project_name=name).encode('utf-8'))
        fp.close()


if __name__ == "__main__":
    main()
